package org.groupg.project.a;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.text.SimpleDateFormat;
import java.time.LocalDate;

public class XmlProcessor {
    private static final XmlMapper xmlMapper = new XmlMapper();

    static {
        // XML 配置
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
        xmlMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        // 注册模块
        xmlMapper.registerModule(new JavaTimeModule());

        // 自定义序列化器模块
        SimpleModule customModule = new SimpleModule();
        customModule.addSerializer(LocalDate.class, new CustomDateSerializer());
        customModule.addDeserializer(LocalDate.class, new CustomDateDeserializer());
        xmlMapper.registerModule(customModule);
    }

    public static String toXml(Employee employee) throws JsonProcessingException {
        return xmlMapper.writeValueAsString(employee);
    }

    public static Employee fromXml(String xml) throws JsonProcessingException {
        return xmlMapper.readValue(xml, Employee.class);
    }
}